package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.AppMachDataStatDTO;
import com.whfc.mach.dto.MachDayDataDTO;
import com.whfc.mach.dto.MachMonthReportDTO;
import com.whfc.ms.api.mach.service.MsReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author sun_guodong
 * @description 设备报表
 * @date 2021-03-10
 */
@RestController
@RequestMapping("/ms/api/v2/mach")
public class MsReportController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsReportService reportService;

    @GetMapping("/dayReport")
    public Result dayReport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("deptId") Integer deptId,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize) {
        logger.info("设备日报,loginer:{},deptId:{},date:{},pageNum:{},pageSize:{}",
                user.getUsername(), deptId, date, pageNum, pageSize);
        PageData<MachDayDataDTO> pageData = reportService.dayReport(deptId, date, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/report", params = {"month"})
    public Result monthReport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("设备月报,loginer:{},deptId:{},pageNum:{},pageSize:{},month:{}",
                user.getUsername(), deptId, pageNum, pageSize, month);
        PageData<MachMonthReportDTO> pageData = reportService.monthReport(deptId, pageNum, pageSize, month);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/report", params = {"startDate", "endDate"})
    public Result commonReport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("deptId") Integer deptId,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("设备多天数据报表,loginer:{},deptId:{},pageNum:{},pageSize:{},startDate:{},endDate:{}",
                user.getUsername(), deptId, pageNum, pageSize, startDate, endDate);
        PageData<MachMonthReportDTO> pageData = reportService.commonReport(deptId, pageNum, pageSize, startDate, endDate);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/report", params = {"year"})
    public Result yearReport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("year") Integer year) {
        logger.info("设备年报,loginer:{},deptId:{},pageNum:{},pageSize:{},year:{}",
                user.getUsername(), deptId, pageNum, pageSize, year);
        PageData<MachMonthReportDTO> pageData = reportService.yearReport(deptId, pageNum, pageSize, year);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/report/stat", params = {"date"})
    public Result dayReportStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("设备日报数据统计,loginer:{},deptId:{},date:{}", user.getUsername(), deptId, date);
        AppMachDataStatDTO data = reportService.dayReportStat(deptId, date);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/report/stat", params = {"month"})
    public Result monthReportStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("deptId") Integer deptId,
                                  @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("设备月报数据统计,loginer:{},deptId:{},month:{}", user.getUsername(), deptId, month);
        AppMachDataStatDTO data = reportService.monthReportStat(deptId, month);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/report/stat", params = {"startDate", "endDate"})
    public Result reportStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("设备多日报表数据统计,loginer:{},deptId:{},startDate:{},endDate:{}", user.getUsername(), deptId, startDate, endDate);
        AppMachDataStatDTO data = reportService.reportStat(deptId, startDate, endDate);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/report/stat", params = {"year"})
    public Result yearReportStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("deptId") Integer deptId,
                                 @RequestParam("year") Integer year) {
        logger.info("设备年报数据统计,loginer:{},deptId:{},year:{}", user.getUsername(), deptId, year);
        AppMachDataStatDTO data = reportService.yearReportStat(deptId, year);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/report/export", params = {"date"})
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("导出设备日报,loginer:{},deptId:{},date:{}", user.getUsername(), deptId, date);
        OssPathDTO data = reportService.dayReportExport(deptId, date);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/report/export", params = {"month"})
    public Result monthReportExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam("deptId") Integer deptId,
                                    @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("导出设备月报,loginer:{},deptId:{},month:{}", user.getUsername(), deptId, month);
        OssPathDTO data = reportService.monthReportExport(deptId, month);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/report/export", params = {"startDate", "endDate"})
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("导出设备多天报表,loginer:{},deptId:{},startDate:{},endDate:{}", user.getUsername(), deptId, startDate, endDate);
        OssPathDTO data = reportService.export(deptId, startDate, endDate);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/report/export", params = {"year"})
    public Result yearReportExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                   @RequestParam("deptId") Integer deptId,
                                   @RequestParam("year") Integer year) {
        logger.info("导出设备年报,loginer:{},deptId:{},year:{}", user.getUsername(), deptId, year);
        OssPathDTO data = reportService.yearReportExport(deptId, year);
        return ResultUtil.success(data);
    }


    /*********************************导出普通设备报表***********************************************/
    @PostMapping(value = "/common/report/export", params = {"date"})
    public Result commonExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("deptId") Integer deptId,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("导出普通设备日报,loginer:{},deptId:{},date:{}", user.getUsername(), deptId, date);
        OssPathDTO data = reportService.commonDayReportExport(deptId, date);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/common/report/export", params = {"month"})
    public Result commonMonthReportExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                          @RequestParam("deptId") Integer deptId,
                                          @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("导出普通设备月报,loginer:{},deptId:{},month:{}", user.getUsername(), deptId, month);
        OssPathDTO data = reportService.commonMonthReportExport(deptId, month);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/common/report/export", params = {"startDate", "endDate"})
    public Result commonExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("deptId") Integer deptId,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("导出普通设备多天报表,loginer:{},deptId:{},startDate:{},endDate:{}", user.getUsername(), deptId, startDate, endDate);
        OssPathDTO data = reportService.commonExport(deptId, startDate, endDate);
        return ResultUtil.success(data);
    }


}
